﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Windows.Data;
using System.Windows.Shapes;
using System.Windows.Media;
using myLiveMesh.Views.WindowManager;
using myLiveMesh.Views.Auth;
using System.Windows.Media.Imaging;

namespace myLiveMesh.ViewModels.WindowManager
{      
    public class windowManagerViewModel : viewModelBase
    {

        #region fields

        private int _zIndex;
        private ObservableCollection<windowView> _windows;
        private windowView _currentControl;
        private double x;
        private double y;
        private BitmapImage _backGround;

        #endregion

        #region constructors

        public windowManagerViewModel()
        {
            this._backGround = new BitmapImage(new Uri(@"F:\project\epitech\myLiveMesh\myLiveMesh\Images\hebus_216371_1440x900.jpg", UriKind.RelativeOrAbsolute));
            this.x = 1.0;
            this.y = 1.0;
            this._zIndex = 1;
            this._windows = new ObservableCollection<windowView>();
            this.addWindow(255, 255, 255, 0, "window1");
            this.addWindow(255,255,0,255, "window2");
        }

        #endregion

        #region members

        public ObservableCollection<windowView> Windows
        {
            get { return this._windows; }
        }

        public void addWindow(byte a, byte r, byte g, byte b, string name)
        {
            windowView window = new windowView();
            windowViewModel w = new windowViewModel();
            AuthView av = new AuthView();

            w.Title = "toto";
            window.Name = name;
            window.DataContext = w;
            window.wBehavior.windowClick += clickOnWindow;
            window.btCloseBehaviour1.WindowClosed += this.RemoveWindow;
            window.WinContent.Content = av;
            this.Windows.Add(window);
            Canvas.SetLeft(window, x);
            Canvas.SetTop(window, y);
            Canvas.SetZIndex(window, this._zIndex++);
            this._currentControl = window;
            x += 10;
            y += 10;
        }

        #endregion

        public void RemoveWindow(object sender, RoutedEventArgs e)
        {
            Button bt = sender as Button;

            if (bt != null)
            {
                StackPanel pannel = bt.Parent as StackPanel;
                if (pannel != null)
                {
                    Grid grid = pannel.Parent as Grid;
                    if (grid != null)
                    {
                        windowView window = grid.Parent as windowView;

                        if (window != null)
                        {
                            window.btCloseBehaviour1.WindowClosed -= this.RemoveWindow;
                            this.Windows.Remove(window);
                        }
                    }
                }
            }
        }

        public void clickOnWindow(object sender, RoutedEventArgs e)
        {
            windowView window = sender as windowView;

            if (window != null)
            {
                this._currentControl = window;
                Canvas.SetZIndex(window, this._zIndex++);
            }
        }

        public BitmapImage BackGround
        {
            get { return this._backGround; }
            set { this._backGround = value; }
        }
    } 

}
